package com.dhcc.android.base.common.view;

import android.app.Activity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dhcc.android.base.R;
import com.dhcc.android.base.utils.ClearEditText;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.StringUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 输入框弹出窗
 *
 * @author zhoudan
 * @date 2015年5月20日
 */
public class InputPopupWindow {

    private Activity activity;

    private MyPopupWindow poupWindow;
    private LinearLayout cancle;
    private LinearLayout ok;
    private ClearEditText input;
    private TextView title;
    private LinearLayout body;
    private int pos1, pos2;
    private TextView target;

    private boolean isMust;
    private int type;

    private boolean flag = true;
    private String oldValue;

    private InputMethodManager inputManager;

    public InputPopupWindow(Activity activity) {
        this.activity = activity;
    }

    private ClickListenerInterface clickListenerInterface;

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    public interface ClickListenerInterface {
        public void doConfirm(String value);

        public void doCancel();
    }

    /**
     * 显示弹出输入框
     *
     * @param parent 弹出窗附着视图
     * @param target 需要编辑的textview
     * @param label  标签
     * @param isMust 是否必填
     * @param type   类型 0--没有需求 ，1--数值
     */
    public void showInputPoupWindow(View parent, TextView target, String label,
                                    boolean isMust, int type, boolean isOutsideTouchable) {

        this.isMust = isMust;
        this.type = type;
        this.target = target;
        View v = null;
        if (poupWindow == null) {
            v = LayoutInflater.from(activity).inflate(
                    R.layout.util_input_poup_window, null);

            poupWindow = new MyPopupWindow(v, Constants.screenWidth,
                    Constants.screenHight, isOutsideTouchable);

            body = (LinearLayout) v.findViewById(R.id.poup_body);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.topMargin = Constants.screenHight / 4;
            lp.leftMargin = Constants.screenWidth / 16;
            lp.rightMargin = Constants.screenWidth / 16;

            body.setLayoutParams(lp);

            title = (TextView) v.findViewById(R.id.poup_title);
            input = (ClearEditText) v.findViewById(R.id.poup_input);
            cancle = (LinearLayout) v.findViewById(R.id.poup_cancle);
            ok = (LinearLayout) v.findViewById(R.id.poup_ok);
            //			v.setFocusableInTouchMode(true);
            v.setOnKeyListener(onKeyListener);
            cancle.setOnClickListener(itemClickListener);
            ok.setOnClickListener(itemClickListener);
        }

        title.setText(label);

        switch (type) {
            case 0:
                input.setInputType(EditorInfo.TYPE_CLASS_TEXT);
                break;
            case 1:
                input.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                break;
            default:
                break;
        }

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                inputManager = (InputMethodManager) input.getContext()
                        .getSystemService(activity.INPUT_METHOD_SERVICE);
                // inputManager.showSoftInput(editText, 0);
                inputManager
                        .toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);
            }
        }, 100);
        String oldValue = "";
        if (target != null) {
            oldValue = target.getText().toString();
        }
        input.setText(oldValue);
        input.setSelection(input.getText().length());
        poupWindow
                .showAtLocation(parent, Gravity.CENTER | Gravity.CENTER, 0, 0);
        input.setOnEditorActionListener(new EditText.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NONE
                        || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    System.out.println("点击完成了");
                    ok();
                    return true;
                }
                return false;
            }
        });

    }

    OnKeyListener onKeyListener = new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            System.out.println("点啦");
            return false;
        }
    };
    OnClickListener itemClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.poup_cancle) {
                if (StringUtil.isFastDoubleClick(500)) {
                    return;
                }
                dismiss();
            } else if (v.getId() == R.id.poup_ok) {
                if (StringUtil.isFastDoubleClick(500)) {
                    return;
                }
                ok();
            }
        }
    };

    public void ok() {
        if (flag) {
            flag = false;

            String msg = "";
            String value = input.getText().toString();
            if (StringUtil.isEmpty(value)) {
                if (isMust) {
                    msg = "内容不能为空！";
                    Toast toast = Toast.makeText(activity, msg,
                            Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.TOP, 0,
                            Constants.screenHight / 5);
                    toast.show();
                } else {
                    dismiss();
                }
            } else {
                if (target != null) {
                    target.setText(value);
                } else {
                    if (clickListenerInterface != null) {
                        clickListenerInterface.doConfirm(value);//外部应用
                    }
                }
                poupWindow.dismiss();

            }
            flag = true;
        }
    }

    public void dismiss() {
        if (clickListenerInterface != null) {
            clickListenerInterface.doCancel();//外部应用
        }
        poupWindow.dismiss();
    }
}
